package com.xqboss.web.controller.system;

import com.xqboss.apps.domain.sys.SysPopup;
import com.xqboss.apps.dto.system.GetPopupListDto;
import com.xqboss.apps.dto.system.PopupSaleableDto;
import com.xqboss.apps.enums.sys.PopupJumpEnum;
import com.xqboss.apps.service.sys.SysPopupService;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.enums.BusinessType;
import com.xqboss.common.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = {"弹窗管理"})
@Slf4j
@RestController("WebSysPopupController")
@RequestMapping("/web/popup")
public class SysPopupController extends BaseController {

    @Autowired
    private SysPopupService popupService;

    @PreAuthorize("@ss.hasPermi('web:popup:add')")
    @ApiOperation("新增弹窗")
    @PostMapping("add")
    @Log(title = "弹窗", businessType = BusinessType.INSERT)
    public R add(@Validated @RequestBody SysPopup popup) {
        popup.setCreateUserId(getUserId());
        popup.setCreateUserName(getUsername());
        popupService.addPopup(popup);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:popup:detail')")
    @ApiOperation("弹窗详情")
    @GetMapping("detail/{popupId}")
    public R<SysPopup> findByKey(@PathVariable("popupId")
                                 @ApiParam(value = "弹窗ID", required = true)
                                 @NotNull(message = "弹窗ID不能为空")
                                 Long popupId) {
        SysPopup popup = popupService.getPopupDetail(popupId);
        return R.ok(popup);
    }

    @PreAuthorize("@ss.hasPermi('web:popup:update')")
    @ApiOperation("编辑弹窗")
    @PutMapping("update")
    @Log(title = "弹窗", businessType = BusinessType.UPDATE)
    public R update(@Validated @RequestBody SysPopup popup) {
        popupService.editPopup(popup);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:popup:update-saleable')")
    @ApiOperation("上下架")
    @PutMapping("update-saleable")
    @Log(title = "弹窗", businessType = BusinessType.UPDATE)
    public R updateSaleable(@Validated @RequestBody PopupSaleableDto popup) {
        popupService.changeSaleable(popup.getId(), popup.getSaleable());
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:popup:list')")
    @ApiOperation("弹窗列表")
    @GetMapping("list")
    public R<List<SysPopup>> list(@Validated GetPopupListDto dto) {
        dto.startPageAndOrder();
        List<SysPopup> list = popupService.getPopupList(dto);
        return R.ok(list);
    }

}
